package com.aaa.controller;

import com.aaa.entity.Authority;
import com.aaa.entity.Menu;
import com.aaa.entity.Role;
import com.aaa.service.impl.AuthorityServiceImpl;
import com.aaa.service.impl.RoleServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.awt.print.Book;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: RoleController
 * @Description: TODO
 * @Author: 86139
 * @Date: 2021/9/5 8:28
 * @Version:1.0
 **/
@RestController
@RequestMapping("role")
public class RoleController {
    @Resource
    RoleServiceImpl roleService;
    @Resource
    AuthorityServiceImpl authorityService;
    @RequestMapping("finAll")
    public PageInfo finAll(Integer pageNum,Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Role> list = roleService.list();
        PageInfo<Role> rolePageInfo = new PageInfo<Role>(list);
        return rolePageInfo;
    }
    @RequestMapping("state")
    public boolean state(Role role){
        return roleService.updateById(role);
    }
    @RequestMapping("authority")
    public Integer finAll(Integer rid,@RequestParam(value="authority",required=false) Integer[] authority){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("rid",rid);
        boolean remove = authorityService.remove(queryWrapper);
        if (remove) {
            boolean save = false;
            for (Integer mid : authority) {
                Authority authority1 = new Authority(null, rid, mid);
                save = authorityService.save(authority1);
            }
            if (save) {
                return 1;
            } else {
                return 0;
            }
        }
        return 0;
    }
    @RequestMapping("add")
    public boolean add(Role role){
        List<Map<String,Object>> list = roleService.addAfter(role.getRname());
        if(list.size()>0){
            return false;
        }
        role.setState(0);
        return roleService.save(role);
    }
    @RequestMapping("update")
    public boolean update(Role role){

        QueryWrapper<Role> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("mname",role.getRname());
        List<Map<String,Object>> list = roleService.listMaps(objectQueryWrapper);
        System.out.println(list);
        if(list.size()>0){
            QueryWrapper<Role> wrapper = new QueryWrapper<>();
            objectQueryWrapper.eq("mid",role.getRid());
            Map<String,Object> list1 = roleService.listMaps(wrapper).get(0);
            if(list1.get("rname").equals(role.getRname())){
                return roleService.updateById(role);
            }
            return false;
        }else {
            return roleService.updateById(role);
        }
    }
}
